package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.bean.personage.AlertNameBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.AlertNameServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.wenyu.wenyuyuedu.R.id.et_clear;

/*
* 修改昵称
* */
public class AlertNameActivity extends AppCompatActivity implements View.OnClickListener {


    @InjectView(R.id.member_left)
    ImageView memberLeft;
    @InjectView(R.id.meassage_comment_clear)
    TextView meassageCommentClear;
    @InjectView(R.id.et_alertname)
    EditText etAlertname;
    @InjectView(et_clear)
    ImageView etClear;
    private Matcher matcher;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_name);
        StatusBarUtil.setStatusBar(AlertNameActivity.this, R.color.white);
        ButterKnife.inject(this);
        this.activity = AlertNameActivity.this;
        initView();

    }


    /*
    * 初始化控件的点击事件
    * */
    private void initView() {
        memberLeft.setOnClickListener(this);
        meassageCommentClear.setOnClickListener(this);
        etClear.setOnClickListener(this);
        String nickName = AbSharedUtil.getString(activity, SharedConstants.USER_NICKNAME);
        if (nickName != null) {
            etAlertname.setHint(nickName);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.member_left:
                finish();
                break;
            //确定
            case R.id.meassage_comment_clear:
                requestData();
                break;
            /*清空内容*/
            case et_clear:
                etAlertname.setText("");
                break;
        }
    }

    /*修改昵称的接口*/
    private void requestData() {
        String token = AbSharedUtil.getString(AlertNameActivity.this, SharedConstants.USER_TOKEN);
        String trim = etAlertname.getText().toString().trim();
        if (trim.equals("")) {
            MyToast.showToast(AlertNameActivity.this, "请输入名称", Toast.LENGTH_SHORT);
        } else {
            compileExChar(trim);
            boolean b = matcher.find();
            if (b) {
                MyToast.showToast(AlertNameActivity.this, "不允许输入特殊字符", Toast.LENGTH_SHORT);
            } else {
                AlertNameServer alertNameServer = ApiWrapper.getInstance().create(AlertNameServer.class);
                Call<AlertNameBean> alertName = alertNameServer.getAlertName(token, trim);
                alertName.enqueue(new Callback<AlertNameBean>() {
                    @Override
                    public void onResponse(Call<AlertNameBean> call, Response<AlertNameBean> response) {
                        AlertNameBean body = response.body();
                        if (body != null) {
                            int code = body.getCode();
                            if (code == 1) {
                                finish();
                                MyToast.showToast(AlertNameActivity.this, "修改昵称成功", Toast.LENGTH_SHORT);
                            } else if (code == 0) {
                                //token错误或者已失效
                                showLoginDoalog();
                            } else if (code == 5200) {
                                //昵称长度不符合要求
                                MyToast.showToast(AlertNameActivity.this, "昵称长度不符合要求", Toast.LENGTH_SHORT);
                            } else if (code == 5201) {
                                //该昵称禁止注册
                                MyToast.showToast(AlertNameActivity.this, "该昵称禁止注册", Toast.LENGTH_SHORT);
                            } else if (code == 5202) {
                                //该昵称已经注册
                                MyToast.showToast(AlertNameActivity.this, "该昵称已经注册", Toast.LENGTH_SHORT);
                            } else if (code == 5203) {
                                //系统繁忙，请您稍后再试！
                                MyToast.showToast(AlertNameActivity.this, "系统繁忙，请您稍后再试！", Toast.LENGTH_SHORT);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<AlertNameBean> call, Throwable t) {

                    }
                });
            }
        }


    }

    /**
     * @prama: str 要判断是否包含特殊字符的目标字符串
     */

    private void compileExChar(String str) {
        String limitEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern pattern = Pattern.compile(limitEx);
        matcher = pattern.matcher(str);
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(AlertNameActivity.this).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(AlertNameActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }
}
